﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IBMS.Common;

namespace IBMS.Page
{
    public partial class Login : System.Web.UI.Page
    {
        private DB db = new DB();
        private User user = new User();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Label1.Visible = false;
            this.Label2.Visible = false;
        }

        protected void btn_login_Click(object sender, EventArgs e)
        {
            string name = this.username.Text.ToString();
            string pwd = this.password.Text.ToString();
            DataTable datatable = db.GetDataTable("select * from Users where Name='"+name+"'");
            if (datatable.Rows.Count == 0)
            {
                this.Label1.Visible = true;
            }
            else
            {
                if (datatable.Rows[0]["Password"].ToString().Equals(user.MD5(pwd)))
                {
                    user.Name = name;
                    user.Password = user.MD5(pwd);
                    user.RealName = datatable.Rows[0]["RealName"].ToString();
                    user.ProjectId = datatable.Rows[0]["ProjectId"].ToString();
                    user.Id = int.Parse(datatable.Rows[0]["Id"].ToString());
                    user.UserType=datatable.Rows[0]["UserType"].ToString();
                    Session["User"] = user;
                    switch (datatable.Rows[0]["UserType"].ToString())
                    {
                        case "SuperAdmin":
                            Response.Redirect("SuperAdminHomePage.aspx");
                            break;
                        case "AuthorizedUser":
                            Response.Redirect("AuthorizedUserHomePage.aspx");
                            break;
                        case "ExaminationUser":
                            Response.Redirect("ExaminationUserHomePage.aspx");
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    this.Label2.Visible = true;
                }
            }
        }

        
    }
}